package com.marvels.approve.loan.controller;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import com.github.pagehelper.Page;
import com.marvels.approve.loan.dto.ListQueryConditionDto;
import com.marvels.approve.loan.dto.LoanDetailDto;
import com.marvels.approve.loan.service.LoanApplicationService;
import com.marvels.approve.loan.service.LoanListService;
import com.marvels.common.dto.MarvelsResultDTO;
import com.marvels.common.exception.MarvelsBusinessException;
import com.marvels.common.exception.MarvelsErrorCode;
import com.marvels.common.utils.MarvelsLogUtil;


@RestController
@RequestMapping("approve/loanApplication/service")
public class LoanApplicationController {

	@Autowired
	private LoanListService listQueryService;

	
	@InitBinder
	public void initBinder(WebDataBinder binder) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		dateFormat.setLenient(false);
		binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
	}
	
	
	/**
	 * 财务处理-放款申请-list查询
	 * @param listQueryConditionDto
	 * @return
	 */
	@Autowired
	private LoanApplicationService  loanApplicationService;
	@RequestMapping("listQuery")
	@ResponseBody
	public MarvelsResultDTO listQuery(@RequestBody ListQueryConditionDto listQueryConditionDto) {
		MarvelsLogUtil.getInstance().info("/approve/finance/loanApplication>>>>>>>>>>>>" + listQueryConditionDto);
		try {
			// 查询状态为P05的数据
			listQueryConditionDto.setPaymentStatus("P05");
			Page<LoanDetailDto> list = listQueryService.queryLoanList(listQueryConditionDto);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(),
					list);
		} catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().error(">>>>>>财务处理-放款申请-list查询FAIL:", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(), e.getMessage(), null);
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error(">>>>>>财务处理-放款申请-list查询异常:", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), MarvelsErrorCode.ERROR.getDesc(),
					null);
		}

	}

	/**
	 * 财务处理-放款申请-批量导入
	 * @param listQueryConditionDto
	 * @return
	 */
	@RequestMapping(value = "applyLoanUpload", method = RequestMethod.POST)
	@ResponseBody
	public MarvelsResultDTO applyLoanUpload(@RequestParam(value = "file") MultipartFile file) {
		try {
			String fileName=file.getOriginalFilename();
			Workbook workbook = null;
			if (fileName.endsWith(".xls")) {
				workbook = new HSSFWorkbook(file.getInputStream());// Excel 2003
			}else if(fileName.endsWith(".xlsx")){
				workbook = new XSSFWorkbook(file.getInputStream());
			}else{
				return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(),"请上传.xls或.xlsx格式的文件",
						null);
			}
			
			return loanApplicationService.applyLoanUploadDeal(workbook);
			
		}catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().error(">>>>>>财务处理-放款申请-批量导入FAIL:", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(), e.getMessage(),
					null);
		}catch (Exception e) {
			MarvelsLogUtil.getInstance().error(">>>>>>财务处理-放款申请-批量导入异常:", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), MarvelsErrorCode.ERROR.getDesc(),
					null);
		}

	}

	/**
	 * 财务处理-放款申请-确认放款操作
	 * @param listQueryConditionDto
	 * @return
	 */
	@RequestMapping("loanOperation")
	@ResponseBody
	public MarvelsResultDTO loanOperation(@RequestBody String loands) {
		try {
			return loanApplicationService.executeLoanApply(loands);
		}catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().error(">>>>>>>财务处理-放款申请-确认放款操作FAIL:", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(), e.getMessage(),
					null);
		}catch (Exception e) {
			MarvelsLogUtil.getInstance().error(">>>>>>财务处理-放款申请-确认放款操作异常:", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), "放款处理异常", null);
		}
	}
}
